package com.itheima.homework.demoddz;

import java.util.*;

public class DemoX {
    private static HashSet<Card> set=new HashSet<>(); //一副牌
    public static void main(String[] args) {
        allCards();
        System.out.print("A:");
        getCards().stream().forEach(s-> {
          s.setShow(getName(s.getNum()));
            System.out.print(s.getShow()+s.getFace()+" ");});
        System.out.print("\nB:");
        getCards().stream().forEach(s-> {
            s.setShow(getName(s.getNum()));
            System.out.print(s.getShow()+s.getFace()+" ");});
        System.out.print("\nC:");
        getCards().stream().forEach(s-> {
            s.setShow(getName(s.getNum()));
            System.out.print(s.getShow()+s.getFace()+" ");});
        System.out.print("\n剩余的底牌是：");
        System.out.println(set.size());
        set.stream().forEach(s-> {
            s.setShow(getName(s.getNum()));
            System.out.print(s.getShow()+s.getFace()+" ");});
    }
    /*
        换名字
     */
    public static String getName(int s){
        if(s==1){
            return "A";
        }
        if(s==11){
            return "J";
        }
        if(s==12){
            return "Q";
        }
        if(s==13){
            return "K";
        }
        if(s==14){
            return "小王";
        }
        if(s==15){
            return "大王";
        }
        return String.valueOf(s);
    }
    /*
        抽牌
     */
    public static TreeSet<Card> getCards(){
        ArrayList<Card> list= new ArrayList<>();
        list.addAll(set);
        Collections.shuffle(list);   //先洗一次牌
        TreeSet<Card> oneR=new TreeSet<>((o1, o2) -> {
            int num=o1.getNum()-(o2.getNum());
            num=num==0?o1.getFace().compareTo(o2.getFace()):num;
            return num;
        });
        Iterator<Card> it = list.iterator();
        int x=0;
        while (it.hasNext()){
            if(x>16) break;
            Card card = it.next();
            oneR.add(card);
            x++;
        }
        set.removeAll(oneR);
        return oneR;
    }
    /*
        生成一副牌
     */
    public static HashSet<Card> allCards(){
        int i=1,k=1,l=1;//k表示形状//l表示数字
        while (i<=52){
            Card c=new Card();
            if(k==1){
                c.setFace("♥");
                c.setColor("红色");
                k++;
            }else if(k==2){
                c.setFace("♦");
                c.setColor("红色");
                k++;
            }else if(k==3){
                c.setFace("♣");
                c.setColor("黑色");
                k++;
            }else {
                c.setFace("♠");
                c.setColor("黑色");
                k++;
                k=1;
            }
            if(l<=13){
                c.setNum(l);
            }else {
                l=1;
                c.setNum(l);
            }
            set.add(c);
            i++; l++;
        }
        set.add(new Card(14,"红色","BigKingdom"));
        set.add(new Card(15,"黑色","SmallKingdom"));
        System.out.println("总牌数:"+set.size());
        return set;
    }
}
